/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.survey.web.SurveyAction
   Module Description   :

   Date Created      : 2010/11/10
   Original Author   : feeling.wang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.survey.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.survey.entity.Survey;
import com.yaodian100.ec.survey.entity.SurveyCoMaping;
import com.yaodian100.ec.survey.service.SurveyService;

/**
 * @author feeling.wang
 * 
 */
public class SurveyAction extends ActionSupport implements Preparable {
	private static final long serialVersionUID = -5848194974665996953L;
	private final Logger logger = LoggerFactory.getLogger(SurveyAction.class);
	@Autowired
	private SurveyService surveyService;
	private Survey survey;
	private SurveyCoMaping surveyCoMaping;
	private List<Survey> surveyList;
	private Map<String, String> coNameMap;
	private SimplePager pager;

	/* search */
	private String nameParam;
	private String startDateParam;
	private String endDateParam;

	/* edit */
	private String id;
	private String name;
	private String startDate;
	private String endDate;
	private String template;
	private String coName1;
	private String coName2;
	private String coName3;
	private String coName4;
	private String coName5;
	private String coName6;
	private String coName7;
	private String coName8;
	private String coName9;
	private String coName10;
	private String coName11;
	private String coName12;
	private String coName13;
	private String coName14;
	private String coName15;
	private String coName16;
	private String coName17;
	private String coName18;
	private String coName19;
	private String coName20;
	private String coName21;
	private String coName22;
	private String coName23;
	private String coName24;
	private String coName25;
	private String coName26;
	private String coName27;
	private String coName28;
	private String coName29;
	private String coName30;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		clearErrorsAndMessages();
		if (pager == null) {
			pager = new SimplePager();
		}
		pager.setPageRecord(20);// defaut 20

	}

	public String add() throws Exception {
		id = "";
		survey = null;
		surveyCoMaping = null;
		coNameMap = null;
		return "edit";
	}

	public String search() throws Exception {
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isNotBlank(nameParam)) {
			crit.addRlike("name", nameParam);
		}
		DateFormat dateFormat = new SimpleDateFormat(EcConstant.PATTERN_DATE);
		if (StringUtils.isNotBlank(startDateParam)) {
			Date sday = dateFormat.parse(startDateParam);
			crit.addGe("startDate", sday);
		}
		if (StringUtils.isNotBlank(endDateParam)) {
			Date eday = dateFormat.parse(endDateParam);
			crit.addLe("endDate", eday);
		}
		int entitySize = surveyService.getListSize(crit).intValue();
		pager.setTotalSize(entitySize);
		surveyList = surveyService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), crit,
				new String[] { "startDate desc" });
		return "list";
	}

	public String save() throws Exception {
		try {
			if (StringUtils.isBlank(id)) {
				survey = new Survey();
				surveyCoMaping = new SurveyCoMaping();
			} else {
				survey = surveyService.getSurvey(id);
				surveyCoMaping = surveyService.getSurveyCoMaping(id);

			}
			survey.setStartDate(DateUtil.convertStringToDate(startDate));
			survey.setEndDate(DateUtil.convertStringToDate(endDate));
			survey.setName(name);
			survey.setTemplate(template);
			if (null != surveyCoMaping) {
				surveyCoMaping.setCoName1(coName1);
				surveyCoMaping.setCoName2(coName2);
				surveyCoMaping.setCoName3(coName3);
				surveyCoMaping.setCoName4(coName4);
				surveyCoMaping.setCoName5(coName5);
				surveyCoMaping.setCoName6(coName6);
				surveyCoMaping.setCoName7(coName7);
				surveyCoMaping.setCoName8(coName8);
				surveyCoMaping.setCoName9(coName9);
				surveyCoMaping.setCoName10(coName10);
				surveyCoMaping.setCoName11(coName11);
				surveyCoMaping.setCoName12(coName12);
				surveyCoMaping.setCoName13(coName13);
				surveyCoMaping.setCoName14(coName14);
				surveyCoMaping.setCoName15(coName15);
				surveyCoMaping.setCoName16(coName16);
				surveyCoMaping.setCoName17(coName17);
				surveyCoMaping.setCoName18(coName18);
				surveyCoMaping.setCoName19(coName19);
				surveyCoMaping.setCoName20(coName20);
				surveyCoMaping.setCoName21(coName21);
				surveyCoMaping.setCoName22(coName22);
				surveyCoMaping.setCoName23(coName23);
				surveyCoMaping.setCoName24(coName24);
				surveyCoMaping.setCoName25(coName25);
				surveyCoMaping.setCoName26(coName26);
				surveyCoMaping.setCoName27(coName27);
				surveyCoMaping.setCoName28(coName28);
				surveyCoMaping.setCoName29(coName29);
				surveyCoMaping.setCoName30(coName30);
			}
			survey = surveyService.saveOrUpdateSurvey(survey, surveyCoMaping);
			addActionMessage("储存成功");
		} catch (Exception e) {
			addActionError("储存失败");
			survey.setId(null);
		}
		return edit();
	}

	public String edit() throws Exception {
		if (StringUtils.isBlank(id)) {
			addActionError("传入参数错误,id is null");
		} else {
			survey = surveyService.getSurvey(id);
			surveyCoMaping = surveyService.getSurveyCoMaping(id);
			if (null != surveyCoMaping) {
				coNameMap = new HashMap<String, String>();
				coNameMap.put("coName1", surveyCoMaping.getCoName1());
				coNameMap.put("coName2", surveyCoMaping.getCoName2());
				coNameMap.put("coName3", surveyCoMaping.getCoName3());
				coNameMap.put("coName4", surveyCoMaping.getCoName4());
				coNameMap.put("coName5", surveyCoMaping.getCoName5());
				coNameMap.put("coName6", surveyCoMaping.getCoName6());
				coNameMap.put("coName7", surveyCoMaping.getCoName7());
				coNameMap.put("coName8", surveyCoMaping.getCoName8());
				coNameMap.put("coName9", surveyCoMaping.getCoName9());
				coNameMap.put("coName10", surveyCoMaping.getCoName10());
				coNameMap.put("coName11", surveyCoMaping.getCoName11());
				coNameMap.put("coName12", surveyCoMaping.getCoName12());
				coNameMap.put("coName13", surveyCoMaping.getCoName13());
				coNameMap.put("coName14", surveyCoMaping.getCoName14());
				coNameMap.put("coName15", surveyCoMaping.getCoName15());
				coNameMap.put("coName16", surveyCoMaping.getCoName16());
				coNameMap.put("coName17", surveyCoMaping.getCoName17());
				coNameMap.put("coName18", surveyCoMaping.getCoName18());
				coNameMap.put("coName19", surveyCoMaping.getCoName19());
				coNameMap.put("coName20", surveyCoMaping.getCoName20());
				coNameMap.put("coName21", surveyCoMaping.getCoName21());
				coNameMap.put("coName22", surveyCoMaping.getCoName22());
				coNameMap.put("coName23", surveyCoMaping.getCoName23());
				coNameMap.put("coName24", surveyCoMaping.getCoName24());
				coNameMap.put("coName25", surveyCoMaping.getCoName25());
				coNameMap.put("coName26", surveyCoMaping.getCoName26());
				coNameMap.put("coName27", surveyCoMaping.getCoName27());
				coNameMap.put("coName28", surveyCoMaping.getCoName28());
				coNameMap.put("coName29", surveyCoMaping.getCoName29());
				coNameMap.put("coName30", surveyCoMaping.getCoName30());
			}
		}
		return "edit";
	}

	public SurveyService getSurveyService() {
		return surveyService;
	}

	public void setSurveyService(SurveyService surveyService) {
		this.surveyService = surveyService;
	}

	public Survey getSurvey() {
		return survey;
	}

	public SurveyCoMaping getSurveyCoMaping() {
		return surveyCoMaping;
	}

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getStartDate() {
		return startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public String getTemplate() {
		return template;
	}

	public String getCoName1() {
		return coName1;
	}

	public String getCoName2() {
		return coName2;
	}

	public String getCoName3() {
		return coName3;
	}

	public String getCoName4() {
		return coName4;
	}

	public String getCoName5() {
		return coName5;
	}

	public String getCoName6() {
		return coName6;
	}

	public String getCoName7() {
		return coName7;
	}

	public String getCoName8() {
		return coName8;
	}

	public String getCoName9() {
		return coName9;
	}

	public String getCoName10() {
		return coName10;
	}

	public String getCoName11() {
		return coName11;
	}

	public String getCoName12() {
		return coName12;
	}

	public String getCoName13() {
		return coName13;
	}

	public String getCoName14() {
		return coName14;
	}

	public String getCoName15() {
		return coName15;
	}

	public String getCoName16() {
		return coName16;
	}

	public String getCoName17() {
		return coName17;
	}

	public String getCoName18() {
		return coName18;
	}

	public String getCoName19() {
		return coName19;
	}

	public String getCoName20() {
		return coName20;
	}

	public String getCoName21() {
		return coName21;
	}

	public String getCoName22() {
		return coName22;
	}

	public String getCoName23() {
		return coName23;
	}

	public String getCoName24() {
		return coName24;
	}

	public String getCoName25() {
		return coName25;
	}

	public String getCoName26() {
		return coName26;
	}

	public String getCoName27() {
		return coName27;
	}

	public String getCoName28() {
		return coName28;
	}

	public String getCoName29() {
		return coName29;
	}

	public String getCoName30() {
		return coName30;
	}

	public void setSurvey(Survey survey) {
		this.survey = survey;
	}

	public void setSurveyCoMaping(SurveyCoMaping surveyCoMaping) {
		this.surveyCoMaping = surveyCoMaping;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public void setTemplate(String template) {
		this.template = template;
	}

	public void setCoName1(String coName1) {
		this.coName1 = coName1;
	}

	public void setCoName2(String coName2) {
		this.coName2 = coName2;
	}

	public void setCoName3(String coName3) {
		this.coName3 = coName3;
	}

	public void setCoName4(String coName4) {
		this.coName4 = coName4;
	}

	public void setCoName5(String coName5) {
		this.coName5 = coName5;
	}

	public void setCoName6(String coName6) {
		this.coName6 = coName6;
	}

	public void setCoName7(String coName7) {
		this.coName7 = coName7;
	}

	public void setCoName8(String coName8) {
		this.coName8 = coName8;
	}

	public void setCoName9(String coName9) {
		this.coName9 = coName9;
	}

	public void setCoName10(String coName10) {
		this.coName10 = coName10;
	}

	public void setCoName11(String coName11) {
		this.coName11 = coName11;
	}

	public void setCoName12(String coName12) {
		this.coName12 = coName12;
	}

	public void setCoName13(String coName13) {
		this.coName13 = coName13;
	}

	public void setCoName14(String coName14) {
		this.coName14 = coName14;
	}

	public void setCoName15(String coName15) {
		this.coName15 = coName15;
	}

	public void setCoName16(String coName16) {
		this.coName16 = coName16;
	}

	public void setCoName17(String coName17) {
		this.coName17 = coName17;
	}

	public void setCoName18(String coName18) {
		this.coName18 = coName18;
	}

	public void setCoName19(String coName19) {
		this.coName19 = coName19;
	}

	public void setCoName20(String coName20) {
		this.coName20 = coName20;
	}

	public void setCoName21(String coName21) {
		this.coName21 = coName21;
	}

	public void setCoName22(String coName22) {
		this.coName22 = coName22;
	}

	public void setCoName23(String coName23) {
		this.coName23 = coName23;
	}

	public void setCoName24(String coName24) {
		this.coName24 = coName24;
	}

	public void setCoName25(String coName25) {
		this.coName25 = coName25;
	}

	public void setCoName26(String coName26) {
		this.coName26 = coName26;
	}

	public void setCoName27(String coName27) {
		this.coName27 = coName27;
	}

	public void setCoName28(String coName28) {
		this.coName28 = coName28;
	}

	public void setCoName29(String coName29) {
		this.coName29 = coName29;
	}

	public void setCoName30(String coName30) {
		this.coName30 = coName30;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public List<Survey> getSurveyList() {
		return surveyList;
	}

	public void setSurveyList(List<Survey> surveyList) {
		this.surveyList = surveyList;
	}

	public Map<String, String> getCoNameMap() {
		return coNameMap;
	}

	public void setCoNameMap(Map<String, String> coNameMap) {
		this.coNameMap = coNameMap;
	}

	public String getNameParam() {
		return nameParam;
	}

	public String getStartDateParam() {
		return startDateParam;
	}

	public String getEndDateParam() {
		return endDateParam;
	}

	public void setNameParam(String nameParam) {
		this.nameParam = nameParam;
	}

	public void setStartDateParam(String startDateParam) {
		this.startDateParam = startDateParam;
	}

	public void setEndDateParam(String endDateParam) {
		this.endDateParam = endDateParam;
	}
}
